<config lang="json">
    {
        "name": "UC2_nip_demo",
        "type": "native-python",
        "version": "0.0.2",
        "api_version": "0.1.2",
        "description": "Minimal working example for the NIP toolbox.",
        "tags": [],
        "ui": [],
        "inputs": null,
        "outputs": null,
        "flags": [],
        "icon": null,
        "env": null,
        "runnable": false,
        "requirements": ["numpy", "matplotlib", "pillow", "scikit-image", "tifffile", "cmd:pip install git+https://NanoImagingUser:NanoImagingUser@gitlab.com/bionanoimaging/nanoimagingpack"],
        "dependencies": []
    }
    </config>
    
    
    
    <script lang="python">
    import numpy as np
    import base64
    import asyncio
    import os
    import time
    from skimage import io
    from PIL import Image
    from io import BytesIO
    
    import NanoImagingPack as nip
    
    import matplotlib as mpl
    mpl.use('Agg')
    import matplotlib.pyplot as plt
    plt.ioff()
    
    class ImJoyPlugin():
        def setup(self):
            self.window = None
    
        async def run(self, my):
            api.showStatus('[UC2] Interface to NIP')
            print(my.config.chart_type)
            # read the raw-hologram
            
            await self.nip_demo()
    
    
        async def nip_demo(self, base64_url):
            ''' Bridging javascript with Python '''
            await api.showMessage('loading image and process it...')
            
            myimage = nip.readim()
            myimage_ft = np.log(1+np.abs(nip.ft(myimage)))
            myimage_ft = myimage_ft/np.max(myimage_ft)
            
    
            # Plot the result and save as png
            fig1, ax = plt.subplots(num='Fouriertransformed Image')
            plt.imshow(myimage_ft,cmap='gray')
            name_plot = 'HOLOGRAM.png'
            plt.savefig(name_plot,dpi=300)
    
            with open(name_plot, 'rb') as f:
                data = f.read()
                result = base64.b64encode(data).decode('ascii')
                imgurl = 'data:image/png;base64,' + result
                data = {"src": imgurl}
                data_plot = {
                    'name':'Plot charts: show png',
                    'type':'imjoy/image',
                    'w':12, 'h':15,
                    'data':data}
    
            # Check if window was defined
            if self.window is None:
                self.window = await api.createWindow(data_plot)
                print(f'Window created')
            else:
                print(f'Update window.')
                try:
                    await self.window.run(data=data)
                except:
                    self.window = await api.createWindow(data_plot)
                    print('Could not print to old window. New window created.')
            
            await api.showMessage('done!')
    
    api.export(ImJoyPlugin())
    
    
    
    
    </script>
    